package com.zyk.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zyk.entity.Role;
import com.zyk.service.IRoleResourceService;
import com.zyk.service.IRoleService;
import com.zyk.vo.DataGridVo;
import com.zyk.vo.FinalResult;
import com.zyk.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author feifeimao
 * @since 2017-11-20
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @Autowired
    private IRoleResourceService roleResourceService;

    /**
     * 跳转到角色管理页面
     */
    @RequestMapping("manager")
    public String manager(){
        return "admin/role/role";
    }

    /**
     * 跳转到添加角色的页面
     */
    @RequestMapping("addPage")
    public String addPage(){
        return "admin/role/roleAdd";
    }

    /**
     * 跳转角色编辑页面
     */
    @RequestMapping("editPage")
    public String editPage(Role role, Model model){
        model.addAttribute("role",roleService.selectById(role.getId()));
        return "admin/role/roleEdit";
    }

    /**
     * 跳转到添加角色的页面
     */
    @RequestMapping("grantPage")
    public String grantPage(Long id,Model model){
        model.addAttribute("id",id);
        return "admin/role/roleGrant";
    }
    /**
     * 添加角色
     * add
     */
    @RequestMapping("add")
    @ResponseBody
    public FinalResult add(Role role){
        if(roleService.insert(role)){
            return new FinalResult("添加成功",true);
        }else {
            return new FinalResult("添加失败",false);
        }
    }

    /**
     * delete
     * 删除角色
     */
    @RequestMapping("delete")
    @ResponseBody
    public FinalResult delete(Role role){
        if(roleService.deleteById(role.getId())){
            return new FinalResult("删除成功",true);
        }else {
            return new FinalResult("删除失败",false);
        }
    }

    /**
     * edit
     * 编辑角色
     *
     */
    @RequestMapping("edit")
    @ResponseBody
    public FinalResult edit(Role role){
        return roleService.edit(role);
    }
    /**
     * DataGridVo
     * @return
     */
    @RequestMapping("dataGrid")
    @ResponseBody
    public DataGridVo dateGrid(){
        List<Role> roles = roleService.selectList(new EntityWrapper<Role>(null));
        return new DataGridVo(roles.size(),roles);
    }


    /**
     * 根据RoleId获取资源列表
     * findResourceIdListByRoleId
     */
    @RequestMapping("findResourceIdListByRoleId")
    @ResponseBody
    public FinalResult findResourceIdListByRoleId(Role role){
        return roleService.findResourceIdListByRoleId(role);
    }

    /**
     * grant
     * 授权
     */
    @RequestMapping("grant")
    @ResponseBody
    public FinalResult grant(Long id,Long[] resourceIds){
        return roleResourceService.grant(id,resourceIds);
    }

    @RequestMapping("tree")
    @ResponseBody
    public List<RoleVo> tree(){
        return roleService.findAll();
    }
}
